package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeSectionMethodService;
import com.bidevalution.business.service.BeSectionReappraisalRecordService;
import com.bidevalution.dao.entity.BeSectionMethod;
import com.bidevalution.dao.entity.BeSectionReappraisalRecord;
import com.bidevalution.support.context.Constants;
import com.qtp.core.model.Response;
import com.qtp.core.redis.RedisUtil;
import com.qtp.core.util.IdsUtil;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 评标标段重评记录管理
 * @author: xiayf
 * @date: 2019-10-08 13:25
 */
@Log4j2
@RestController
@RequestMapping("/beSectionReappraisalRecord")
@Api(description = "评标标段重评记录管理")
public class BeSectionReappraisalRecordController extends BaseController {
    @Autowired
    private BeSectionReappraisalRecordService beSectionReappraisalRecordService;
    @Autowired
    private BeSectionMethodService beSectionMethodService;

    private static final String UPDATE_SUBMIT = "UPDATE_SUBMIT_";


    @ApiOperation(value = "查询重评替补发起记录(2019-11-26-v1)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "submitType", value = "类型（1、替补 2、重评）", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/queryRecordByObject")
    @ApiResponses({
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_ONE, message = "参数不可以为空！"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_TWO, message = "查询重评替补记录失败，请联系管理员！"),
    })
    public Response<BeSectionReappraisalRecord> queryRecordByObject(@RequestParam("sectionId") String sectionId, @RequestParam("submitType") String submitType) {
        try {
            if (StringUtils.isBlank(sectionId) || StringUtils.isBlank(submitType)) {
                return Response.fail("参数不可以为空！", Constants.FIVE_HUNDRED_AND_ONE);
            }
            BeSectionMethod beSectionMethod = beSectionMethodService.selectBySectionId(sectionId);
            if(beSectionMethod == null) {
            	return Response.fail("标段与方法绑定记录不存在");
            }
            BeSectionReappraisalRecord record = new BeSectionReappraisalRecord();
            record.setSectionId(sectionId);
            record.setSubmitType(submitType);
            record.setIsSuccess(Constants.FALSE);
            record.setSubmitCode(beSectionMethod.getSubmitCode());
            return Response.ok(beSectionReappraisalRecordService.queryRecordByObject(record));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("查询重评替补记录失败，请联系管理员！", Constants.FIVE_HUNDRED_AND_TWO);
        }
    }


    /*@ApiOperation(value = "更新版本号(2019-12-01-v1)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping("/updateSubmitCodeById")
    @ApiResponses({
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_ONE, message = "参数不可以为空！"),
    })
    public Response<String> updateSubmitCodeById(@RequestParam("sectionId") String sectionId) {
        try {
            if (StringUtils.isBlank(sectionId)) {
                return Response.fail("参数不可以为空！", Constants.FIVE_HUNDRED_AND_ONE);
            }
            //保证专家重评时只更新一次submitcode,再次重评时会清空redis
            if (StringUtils.isBlank(RedisUtil.get(UPDATE_SUBMIT + sectionId))) {
                String submitCode = IdsUtil.uuid();
                beSectionMethodService.updateByPrimaryKeySelective(sectionId, submitCode);
                RedisUtil.set(UPDATE_SUBMIT + sectionId, submitCode);
                RedisUtil.expire(UPDATE_SUBMIT + sectionId, 60 * 60 * 24);
            }
            return Response.ok("更新成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("更新失败，请联系管理员！", Constants.FIVE_HUNDRED_AND_TWO);
        }
    }*/


}
